package com.pshcp.service;

import com.framework.util.SystemUtil;
import com.pshcp.domain.dto.FeedbackDTO;
import com.pshcp.domain.model.Feedback;
import com.pshcp.mapper.FeedbackMapper;

import com.github.pagehelper.PageHelper;
import com.framework.base.PageList;
import com.framework.tool.CommonTool;
import com.pshcp.common.enums.CommonStatusEnum;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 反馈表 服务实现类
 * </p>
 *
 * @author auto generator
 * @since 2022-11-02
 */
@Service
public class FeedbackService {

    @Autowired
    private FeedbackMapper feedbackMapper;

    public PageList<Feedback> list(FeedbackDTO feedbackDTO){
        if(feedbackDTO.getPageNum() != null && feedbackDTO.getPageSize() != null){
            PageHelper.startPage(feedbackDTO.getPageNum(), feedbackDTO.getPageSize());
        }
        Example example = new Example(Feedback.class);
        Example.Criteria criteria = example.createCriteria();
        if(feedbackDTO.getStatus() != null){
            criteria.andEqualTo("status", feedbackDTO.getStatus());
        }
        example.setOrderByClause(" created_time desc");
        List<Feedback> list = feedbackMapper.selectByExample(example);
        return CommonTool.buildPageList(list);
    }

    public List<Feedback> select(Feedback feedback){
        return feedbackMapper.select(feedback);
    }

    public int insert(Feedback feedback){
        feedback.setId(SystemUtil.getId());
        feedback.setStatus(CommonStatusEnum.INIT.getCode());
        feedback.setCreatedTime(new Date());
        return feedbackMapper.insert(feedback);
    }

    public int update(Feedback feedback){
        feedback.setUpdateTime(new Date());
        return feedbackMapper.updateByPrimaryKeySelective(feedback);
    }

    public Feedback findById(Long id){
        return feedbackMapper.selectByPrimaryKey(id);
    }

    public List<Feedback> findByIds(List<Long> ids){
        Example example = new Example(Feedback.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        return feedbackMapper.selectByExample(example);
    }

    public int delById(Long id){
        Example example = new Example(Feedback.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        Feedback feedback = new Feedback();
        feedback.setStatus(CommonStatusEnum.DEL.getCode());
        return feedbackMapper.updateByExampleSelective(feedback, example);
    }

}
